package com.linhh.mall.center.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linhh.mall.center.api.CollectionService;
import com.linhh.mall.center.common.pojo.BrandInfo;
import com.linhh.mall.center.common.pojo.Collection;
import com.linhh.mall.center.common.pojo.CollectionStatus;
import com.linhh.mall.center.common.pojo.MemberInfo;
import com.linhh.mall.center.server.mappers.CollectionMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author linhh
 * @Date 2023 03 18 11 26
 **/
@Service
public class CollectionServiceImpl implements CollectionService {

    private List<Collection> collections = new ArrayList<>();

    @Autowired
    private CollectionMapper collectionMapper;

    /**
     * 查询所有藏品
     * @return
     */
    @Override
    public List<Collection> getCollectionList() {
        return collectionMapper.selectAll();
    }

    /**
     * 增加
     * @param collection
     * @return
     */
    @Override
    public int addCollection(Collection collection) {
        collectionMapper.insertSelective(collection);
        return 1;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public int deleteCollection(Long id) {
        collectionMapper.deleteByPrimaryKey(id);
        return 1;
    }

    /**
     * 更新藏品信息
     * @param collection
     * @return
     */
    @Override
    public int updateCollection(Collection collection) {
        // 修改藏品
        for(int i=0;i<collections.size();i++){
            if(collections.get(i).getId().equals(collection.getId())){
                collections.set(i,collection);
                break;
            }
        }
        return 1;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Collection getCollectionById(Long id) {
        return collectionMapper.selectByPrimaryKey(id);
    }

    /**
     * 筛选藏品
     * @param member 藏品编码
     * @param name 藏品名称
     * @param status  藏品状态
     * @return
     */
    @Override
    public List<Collection> getCollections(String member, String name, CollectionStatus status) {
        // 根据条件筛选藏品
        List<Collection> result = new ArrayList<>();
        for (Collection collection : collections) {
            if ((member == null || collection.getNumber().equals(member)) && (name == null || collection.getName().equals(name)) && (status == null || collection.getStatus().equals(status))) {
                result.add(collection);
            }
        }
        return result;
    }

    /**
     * 分页查询藏品信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Collection> selectPageInfo(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(Collection.class);
        example.setOrderByClause("id desc");
        List<Collection> collectionList = collectionMapper.selectByExample(example);
        PageInfo<Collection> pageInfo = new PageInfo<>(collectionList);
        return pageInfo;
    }
}
